package research.sbookmarking.input.xml;

import java.util.*;
//import org.afox.util.*;
import org.afox.parsers.xml.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;

import research.sbookmarking.input.Command;

/**
 *  This Controller is used to read in the xml files that contain the commands to be
 *  executed against Connotea.org.
 *  <p>
 *  To get the commands one needs to:
 *  <ol> instantiate this class; and</ol>
 *  <ol> call {@link getCommands} method.</ol>
 *  <p> 
 *  @author <A HREF="mailto:aatleung@gmail.com">Albert Leung</a>
 *  @version 1.0
 *
 */
public class XmlController {

	String filename = null;
	
	/**
	 * This constructor creates an instance of this class when a filename is passed.
	 * <p>
	 * @param filename the filename of the xml file that contains the commands to be executed against
	 * 				   Connotea.org.
	 */
	public XmlController(String filename) {
		this.filename=filename;
	}
	
	/**
	 * This method calls the appropriate methods to extract the commands from the xml file. 
	 * <p>
	 * @return an {@link ArrayList} of {@link Command} objects that can be executed against Connotea.org  
	 */
	public ArrayList getCommands (){
		BaseHandler theHandler = null;
        try {
        	XMLReader xmlParser = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");
        	xmlParser.setContentHandler(theHandler = new BaseHandler("research.sbookmarking.input."));
            xmlParser.parse(new InputSource(filename));
        }
        catch(Exception x) {
        	System.err.println("Exception:" + x);
        	System.exit(-1);
        }
        return (ArrayList)theHandler.getResult();
	}

/*
	public static void main(String[] args) {
		XmlController controller = new XmlController("setting.xml");
		ArrayList results = controller.getCommands();
		
		Command command = (Command)results.get(0);
		command.execute();
		//String url= new String ("http://www.ucalgary.ca");
		//System.out.println(new org.restlet.data.Reference(url));
		//System.out.println("controller: " + command.getResult());
	}
*/
}
